﻿using DearlerPlatfrom.Common.TokenModule;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace DearlerPlatfrom.Common
{
    public class TokenHelper
    {
        public static string CreateToken(JwtTokenModel jwt)
        {
            var claims = new[] {
              new Claim("Id",jwt.Id.ToString()),
              new Claim("CustomerNo",jwt.CustomerNo),
              new Claim("CustomerName",jwt.CustomerName),
            };
            //生成秘钥
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwt.Security));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(
                issuer: jwt.Issuer,
                audience: jwt.Audience,
                expires: DateTime.Now.AddMinutes(jwt.Expires),
                signingCredentials: creds,
                claims: claims
                );
            var accessToken=new JwtSecurityTokenHandler().WriteToken(token);
            return accessToken;
        }
    }
}
